﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace ButtonTree2
{
    public static class RibbonGraphicsUtils
    {
        private static void DrawPath(Graphics g, Pen p, Brush brush, GraphicsPath gp)
        {
            if (brush != null)
            {
                g.FillPath(brush, gp);
            }
            if (p != null)
            {
                g.DrawPath(p, gp);
            }
        }

        // 绘制圆角矩形
        public static void DrawRoundRect(Graphics g, Pen p, Brush brush, Rectangle rect, float radius)
        {
            DrawRoundRect(g, p, brush, rect.Left, rect.Top, rect.Width, rect.Height, radius);
        }

        public static void DrawRoundRect(Graphics g, Pen p, Brush brush, float X, float Y, float width, float height, float radius)
        {
            using(GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
                gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
                gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
                gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
                gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
                gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
                gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
                gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
                gp.CloseFigure();
                DrawPath(g, p, brush, gp);
            }
        }

        // 绘制顶部为圆角的矩形
        public static void DrawTopRoundRect(Graphics g, Pen p, Brush brush, Rectangle rect, float radius)
        {
            DrawTopRoundRect(g, p, brush, rect.Left, rect.Top, rect.Width, rect.Height, radius);
        }

        public static void DrawTopRoundRect(Graphics g, Pen p, Brush brush, float X, float Y, float width, float height, float radius)
        {
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
                gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
                gp.AddLine(X + width, Y + radius, X + width, Y + height);
                gp.AddLine(X + width, Y + height, X, Y + height);
                gp.AddLine(X, Y + height, X, Y + radius);
                gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
                gp.CloseFigure();
                DrawPath(g, p, brush, gp);
            }
        }

        // 绘制底部为圆角的矩形
        public static void DrawBottomRoundRect(Graphics g, Pen p, Brush brush, Rectangle rect, float radius)
        {
            DrawBottomRoundRect(g, p, brush, rect.Left, rect.Top, rect.Width, rect.Height, radius);
        }

        public static void DrawBottomRoundRect(Graphics g, Pen p, Brush brush, float X, float Y, float width, float height, float radius)
        {
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(X, Y, X + width, Y);
                gp.AddLine(X + width, Y, X + width, Y + height - radius);
                gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
                gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
                gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
                gp.AddLine(X, Y + height - radius, X, Y);
                gp.CloseFigure();
                DrawPath(g, p, brush, gp);
            }
        }

        // 绘制左边为圆角的矩形
        public static void DrawLeftRoundRect(Graphics g, Pen p, Brush brush, Rectangle rect, float radius)
        {
            DrawLeftRoundRect(g, p, brush, rect.Left, rect.Top, rect.Width, rect.Height, radius);
        }

        public static void DrawLeftRoundRect(Graphics g, Pen p, Brush brush, float X, float Y, float width, float height, float radius)
        {
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(X + radius, Y, X + width, Y);
                gp.AddLine(X + width, Y, X + width, Y + height);
                gp.AddLine(X + width, Y + height, X + radius, Y + height);
                gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
                gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
                gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
                gp.CloseFigure();
                DrawPath(g, p, brush, gp);
            }
        }

        // 绘制右边为圆角的矩形
        public static void DrawRightRoundRect(Graphics g, Pen p, Brush brush, Rectangle rect, float radius)
        {
            DrawRightRoundRect(g, p, brush, rect.Left, rect.Top, rect.Width, rect.Height, radius);
        }

        public static void DrawRightRoundRect(Graphics g, Pen p, Brush brush, float X, float Y, float width, float height, float radius)
        {
            using (GraphicsPath gp = new GraphicsPath())
            {
                gp.AddLine(X, Y, X + width - (radius * 2), Y);
                gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
                gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
                gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
                gp.AddLine(X + width - (radius * 2), Y + height, X, Y + height);
                gp.AddLine(X, Y + height - (radius * 2), X, Y);
                gp.CloseFigure();
                DrawPath(g, p, brush, gp);
            }
        }


//         // 超强Path计算，生成圆角区域
//         // 参数pts需要满足一个条件，相邻的pt的x,y中有且仅有一个值相等
//         // 参数bRound表示该角是否是圆角
//         public static GraphicsPath CalcRoundPath(Point[] pts, bool[] bRound, float radius, bool bCloseFigure)
//         {
//             int iLength = pts.Length;
// 
//             if (iLength < 2 || bRound.Length != iLength)
//             {
//                 return null;
//             }
// 
//             GraphicsPath path = new GraphicsPath();
// 
//             Point ptPrev = pts[0];
//             Point ptNext;
// 
//             for (int i = 1; i < iLength; ++i)
//             {
//                 if (ptPrev.Y == pts[i].Y)
//                 {
//                     // 横向
//                     int y0 = Math.Min(ptPrev.Y, pts[i].Y);
//                     int y1 = Math.Max(ptPrev.Y, pts[i].Y);
//                     
//                 }
//             }
// 
//             if (bCloseFigure)
//             {
//                 path.CloseFigure();
//             }
// 
//             return path;
//         }

        //--

        public static GraphicsPath GetGraphicsPath_RoundRect_LeftTopRight(Rectangle rect, int radius)
        {
            // 左 上 右 三条直线，加上圆角
            GraphicsPath path = new GraphicsPath();

            int d = 2 * radius;

            path.AddLine(new Point(rect.Left, rect.Bottom), new Point(rect.Left, rect.Top + radius));
            path.AddArc(rect.Left, rect.Top, d, d, 180, 90);
            path.AddLine(new Point(rect.Left + radius, rect.Top), new Point(rect.Right - radius, rect.Top));
            path.AddArc(rect.Right - d, rect.Top, d, d, 270, 90);
            path.AddLine(new Point(rect.Right, rect.Top + radius), new Point(rect.Right, rect.Bottom));

            return path;
        }

        public static void DrawRoundRect_LeftTopRight(Graphics g, Rectangle rect, int radius, Color color)
        {
            using (Pen pen = new Pen(color))
            {
                using (GraphicsPath path = GetGraphicsPath_RoundRect_LeftTopRight(rect, radius))
                {
                    g.DrawPath(pen, path);
                }
            }
        }


        //--
        public static GraphicsPath GetGraphicsPath_RoundRect_LeftBottomRight(Rectangle rect, int radius)
        {
            // 左 下 右 三条直线，加上圆角
            GraphicsPath path = new GraphicsPath();

            int d = 2 * radius;

            path.AddLine(new Point(rect.Left, rect.Top), new Point(rect.Left, rect.Bottom - radius));
            path.AddArc(rect.Left, rect.Bottom - d, d, d, 90, 90);
            path.AddLine(new Point(rect.Left + radius, rect.Bottom), new Point(rect.Right - radius, rect.Bottom));
            path.AddArc(rect.Right - d, rect.Bottom - d, d, d, 0, 90);
            path.AddLine(new Point(rect.Right, rect.Bottom - radius), new Point(rect.Right, rect.Top));

            return path;
        }

        public static void DrawRoundRect_LeftBottomRight(Graphics g, Rectangle rect, int radius, Color color)
        {
            using (Pen pen = new Pen(color))
            {
                using (GraphicsPath path = GetGraphicsPath_RoundRect_LeftBottomRight(rect, radius))
                {
                    g.DrawPath(pen, path);
                }
            }
        }

        //--
    }
}
